package com.example.todolist;

import android.content.Context;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class MyAdapter extends ArrayAdapter {
    public MyAdapter(@NonNull Context context, int resource, ArrayList<HashMap<String,String>> list) {
        super(context, resource,list);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View itemView = convertView;
        if (itemView==null){
            itemView= LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
        }
        Map<String,String> map= (Map<String, String>) getItem(position);
        TextView title=itemView.findViewById(R.id.title);
        TextView detail=itemView.findViewById(R.id.detail);
        TextView month=itemView.findViewById(R.id.month);
        TextView day=itemView.findViewById(R.id.day);


        Log.i("myadapter", "title"+map.get("title")+map.get("detail")+map.get("ddl"));
        title.setText(map.get("title"));
        detail.setText(map.get("detail"));
        String m1=map.get("ddl").substring(6,7);
        String d1=map.get("ddl").substring(8);
        Integer m=Integer.parseInt(m1);
        Integer d=Integer.parseInt(d1);

        String mon="";
        if(m==1){
            mon="Jan";
        }else if(m==2){
            mon="Feb";
        }else if(m==3){
            mon="Mar";
        }else if(m==4){
            mon="Apr";
        }else if(m==5){
            mon="May";
        }else if(m==6){
            mon="Jun";
        }else if(m==7){
            mon="Jul";
        }else if(m==8){
            mon="Aug";
        }else if(m==9){
            mon="Sept";
        }else if(m==10){
            mon="Oct";
        }else if(m==11){
            mon="Nov";
        }else{
            mon="Dec";
        }

        month.setText(mon);
        day.setText(String.valueOf(d));

        return  itemView;

    }
}
